//
//  UIViewController+DismissKeyboard.m
//  DigitalEconomy_iOS
//
//  Created by Ding on 2020/4/22.
//  Copyright © 2020 DingC. All rights reserved.
//

#import "UIViewController+DismissKeyboard.h"


@implementation UIViewController (DismissKeyboard)
- (void)setupForDismissKeyboard {
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    UITapGestureRecognizer *singleTapGR =
    [[UITapGestureRecognizer alloc] initWithTarget:self
                                            action:@selector(tapAnywhereToDismissKeyboard:)];
    
    __weak UIViewController *weakSelf = self;
    NSOperationQueue *mainQuene =[NSOperationQueue mainQueue];
    [nc addObserverForName:UIKeyboardWillShowNotification
                    object:nil
                     queue:mainQuene
                usingBlock:^(NSNotification *note){
                    [weakSelf.view addGestureRecognizer:singleTapGR];
                }];
    [nc addObserverForName:UIKeyboardWillHideNotification
                    object:nil
                     queue:mainQuene
                usingBlock:^(NSNotification *note){
                    [weakSelf.view removeGestureRecognizer:singleTapGR];
                }];
}

- (void)tapAnywhereToDismissKeyboard:(UIGestureRecognizer *)gestureRecognizer {
    //此method会将self.view里所有的subview的first responder都resign掉
    [self.view endEditing:YES];
}

/* 键盘启用 */
- (void)keyboardWillShow:(NSNotification *)notify
               TextY:(CGFloat )textY
{
    //获取键盘弹出前的Rect
     NSValue*keyBoardBeginBounds=[[notify userInfo]objectForKey:UIKeyboardFrameBeginUserInfoKey];
    CGRect beginRect=[keyBoardBeginBounds CGRectValue];
    
    //获取键盘弹出后的Rect
    NSValue*keyBoardEndBounds=[[notify userInfo]objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect  endRect=[keyBoardEndBounds CGRectValue];
    
    //获取键盘位置变化前后纵坐标Y的变化值
    CGFloat deltaY=endRect.origin.y-beginRect.origin.y;
    NSLog(@"%f", deltaY);
    if ((beginRect.size.height > 0 && (beginRect.origin.y - endRect.origin.y > 0))) {
        [UIView animateWithDuration:0.0001f animations:^{
               self.view.frame = CGRectMake(0, 0 - textY, self.view.frame.size.width, self.view.frame.size.height);
        }];
    }
    
//    if (_freshPwdTextfield.isFirstResponder) {
//        [UIView animateWithDuration:0.0001f animations:^{
//            self.view.frame = CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.height);
//        }];
//    }else if (_comfirmPwdTextfield.isFirstResponder) {
//        [UIView animateWithDuration:0.0001f animations:^{
//            self.view.frame = CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height);
//        }];
//    }else{
//    [UIView animateWithDuration:0.0001f animations:^{
//        self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
//    }];
//    }
    [UIView animateWithDuration:0.0001f animations:^{
        self.view.frame = CGRectMake(0, 0 - textY, self.view.frame.size.width, self.view.frame.size.height);
    }];
}

/* 键盘消失 */
- (void)keyboardHiden:(NSNotification *)notify {
    //视图下沉恢复原状
    [UIView animateWithDuration:0.00000000001 animations:^{
        self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    }];
//    [_comfirmPwdTextfield resignFirstResponder];
//    [_freshPwdTextfield resignFirstResponder];
}



/**
*  键盘即将显示的时候调用
*/
- (void)keyboardWillShow:(NSNotification *)note{
    [self keyboardWillShow:note TextY:0];
}

/**
 *  键盘即将退出的时候调用
 */
- (void)keyboardWillHide:(NSNotification *)note{
    [self keyboardHiden:note];
}

@end
